package com.johnny.action;

import java.net.Socket;

import com.johnny.data.DataPool;
import com.johnny.data.PlayerInfo;
import com.johnny.data.RoomInfo;
import com.johnny.startup.Casino28App;

import net.sf.json.JSONObject;

public class EnterRoomAction implements Action {

	@Override
	public String doAction(JSONObject object, Socket socket) {
		JSONObject result=new JSONObject();
		result.put("action", object.getString("action"));
		result.put("state", 0);
		RoomInfo roomInfo = null;
		for (int i = 0; i < DataPool.getInstance().getRoomInfos().size(); i++) {
			if(DataPool.getInstance().getRoomInfos().get(i).getRoomId().equals(object.getString("roomId"))){
				roomInfo=DataPool.getInstance().getRoomInfos().get(i);
				break;
			}
		}
		PlayerInfo playerInfo=Casino28App.connector.getPlayerInfoBySocket(socket);
		if(!roomInfo.getPlayers().contains(playerInfo.getName()))
		{
			roomInfo.getPlayers().add(playerInfo.getName());
		}
		result.put("room", roomInfo);
		return result.toString();
	}

}
